fix(messageutil): preserve zero-valued fields in tool result ToParam (#317, #322)#345
Open
Zeffut wants to merge 1 commit into
Open
fix(messageutil): preserve zero-valued fields in tool result ToParam (#317, #322)#345Zeffut wants to merge 1 commit into
Zeffut wants to merge 1 commit into
Conversation
…nthropics#317, anthropics#322) The ToParam() methods on CodeExecutionToolResultBlock, BashCodeExecutionToolResultBlock, ToolSearchToolResultBlock and their beta counterparts re-marshaled response blocks via struct fields tagged `omitzero`. Zero values from the server (return_code:0, stdout:"", error_code:"") were stripped and the API returned 400 on the next turn. Switch to param.Override[ContentUnion](json.RawMessage(r.Content.RawJSON())) so the exact server-validated payload is forwarded unchanged. This also fixes a silent bug where param.Override was receiving a Go string instead of json.RawMessage — leading to double-encoded JSON. Adds 2 regression tests covering anthropics#317 and anthropics#322. go vet, go build, and the new tests pass cleanly. Closes anthropics#317. Closes anthropics#322. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR addresses regressions where tool result content fields were being dropped during marshaling due to omitzero, causing follow-up API requests to be rejected.
Changes:
- Switch several
ToParam()implementations to raw-JSON passthrough viaparam.Override(...json.RawMessage(...))to preserve zero/empty-but-required fields. - Apply the same raw-JSON preservation behavior to both stable and beta message utilities.
- Add regression tests ensuring required fields like
return_code,stdout/stderr, anderror_coderemain present after round-tripping.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| messageutil_test.go | Adds regression tests validating that required zero/empty fields are preserved in marshaled tool result content. |
| messageutil.go | Replaces typed union construction with raw JSON passthrough in multiple ToParam() methods to avoid omitzero dropping fields. |
| betamessageutil.go | Mirrors the stable fix in beta utilities by using raw JSON passthrough for the same tool result blocks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+51
to
+55
| for _, key := range []string{`"return_code"`, `"stdout"`, `"stderr"`} { | ||
| if !strings.Contains(got, key) { | ||
| t.Errorf("expected %s to be present in marshalled content (got %s)", key, got) | ||
| } | ||
| } |
Comment on lines
+70
to
+72
| if !strings.Contains(string(marshalled), `"error_code"`) { | ||
| t.Errorf("expected error_code to be present in marshalled content (got %s)", string(marshalled)) | ||
| } |
Comment on lines
+319
to
+322
| // Use raw JSON passthrough to preserve fields that would otherwise be | ||
| // dropped by `omitzero` (e.g. zero ReturnCode, empty Stderr/Stdout, or | ||
| // empty ErrorCode), which the API requires on the next turn. See #322. | ||
| p.Content = param.Override[BashCodeExecutionToolResultBlockParamContentUnion](json.RawMessage(r.Content.RawJSON())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
ToParam()methods onCodeExecutionToolResultBlock,BashCodeExecutionToolResultBlock,ToolSearchToolResultBlockand theirBetacounterparts re-marshal server response blocks through struct fields taggedjson:"...,omitzero". Zero-valued but required fields (return_code: 0, emptystdout/stderr, emptyerror_code) are silently dropped, and the next API call returns 400 on the now-malformed content block.Fixes #317 and #322.
Root cause
Two stacked bugs in the manual
messageutil.go/betamessageutil.gofiles (not Stainless-generated):omitzerostrips required zero values.*ResultBlockParamfields are taggedomitzero, so whenToParam()round-trips a server response into params,return_code: 0,stdout: "",stderr: "", anderror_code: ""disappear. The API rejects the resulting block on the next turn.param.Overridewas given a Gostringinstead ofjson.RawMessage. In the existingTextEditorCodeExecutionToolResultBlock.ToParam(and its beta mirror),param.Override[T](r.Content.RawJSON())was passing astring, whichparam.MarshalUnion->shimjson.Marshalthen encodes as a JSON string literal (double-encoded). The documented usage (packages/param/param.go) isjson.RawMessage(...).Fix
Use raw-JSON passthrough on the union content directly:
This forwards the exact server-validated payload unchanged, preserving zero-valued required fields and the variant discriminator without the SDK having to introspect each variant.
Affected functions:
messageutil.go:BashCodeExecutionToolResultBlock.ToParam,CodeExecutionToolResultBlock.ToParam,ToolSearchToolResultBlock.ToParam, andTextEditorCodeExecutionToolResultBlock.ToParam(existing call corrected).betamessageutil.go:BetaBashCodeExecutionToolResultBlock.ToParam,BetaCodeExecutionToolResultBlock.ToParam,BetaToolSearchToolResultBlock.ToParam,BetaTextEditorCodeExecutionToolResultBlock.ToParam(existing call corrected).Tests
Adds two regression sub-tests in
messageutil_test.go(TestContentBlockUnionToParam):stdout/stderr/return_codeforcode_execution_tool_result(CodeExecutionToolResultBlock.ToParam() drops stdout/stderr/return_code via omitzero (same class as #242, #317) #322).error_codefortool_search_tool_result_error(ToolSearchToolResultBlock.ToParam() drops required error_code field via omitzero #317).Test plan
go vet ./...cleango build ./...cleango test -run TestContentBlockUnionToParam ./...passes (all sub-tests)Notes
WebSearchToolResultBlock.ToParam(ToParam() Method Doesn't Handle WebSearchToolResultBlock #242) was intentionally left alone: its union shape diverges (top-level slice variant vs. struct variant) and warrants a separate look.Closes #317.
Closes #322.